package com.huawei.hwclouds.lubanops.demo.product.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huawei.hwclouds.lubanops.demo.common.biz.PageList;
import com.huawei.hwclouds.lubanops.demo.product.biz.model.MerchandiseModel;
import com.huawei.hwclouds.lubanops.demo.product.biz.model.MerchandiseStockModel;
import com.huawei.hwclouds.lubanops.demo.product.biz.param.MerchandiseParam;
import com.huawei.hwclouds.lubanops.demo.product.biz.service.MerchandiseService;
import com.huawei.hwclouds.lubanops.demo.product.client.entity.GetProductDetailResult;
import com.huawei.hwclouds.lubanops.demo.product.client.model.ProductModel;

/**
 * @author y00438022
 * @since 2021/2/26
 **/
@RestController
@RequestMapping(value = "/apm2/demo/product/", produces = "application/json; charset=utf-8")
public class ProductController {

    @Autowired
    private MerchandiseService merchandiseService;

    @GetMapping(value = "")
    public PageList<MerchandiseModel> getProducts(@RequestBody MerchandiseParam param) {
        return merchandiseService.pageQuery(param);
    }

    /**
     * 根据产品ID获取产品详情
     *
     * @param productId
     * @return
     */
    @GetMapping(value = "v1/getProductDetail", produces = {"application/json"})
    public GetProductDetailResult getProductDetail(
        @RequestParam(value = "productId") Integer productId) {
        GetProductDetailResult result = new GetProductDetailResult();
        ProductModel model = new ProductModel();
        model.setProductId(productId);
        model.setProductName("Name get From db");
        result.setProductModel(model);
        return result;
    }

    @PostMapping(value = "/v1/reduce/repertory", produces = {"application/json"})
    public void reduceRepertory(@RequestBody MerchandiseStockModel param) {
        param.setId(null);
        merchandiseService.reduceRepertory(param);
    }

    @PostMapping(value = "/v1/query/info", produces = {"application/json"})
    public List<MerchandiseModel> queryInfo(@RequestBody MerchandiseParam param) {
        return merchandiseService.query(param.getStartNum(), param.getPageSize());
    }
}
